package com.woniuxy.platform.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.platform.entity.Approval;
import com.woniuxy.platform.entity.BusinessInfo;
import com.woniuxy.platform.entity.WuyeInfo;
import com.woniuxy.platform.service.ApprovalService;
import com.woniuxy.platform.service.BusinessInfoService;
import com.woniuxy.platform.service.WuyeInfoService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  定时把需要审核的流程放入审批表中
 * </p>
 *
 * @author 杨青云
 * @since 15:05 2020/9/28
 */
@Component
public class ApprovalJob {

    @Resource
    ApprovalService approvalService;

    @Resource
    WuyeInfoService wuyeInfoService;

    @Resource
    BusinessInfoService businessInfoService;

    @Scheduled(cron = "0/10 * * * * ?")
    public void start(){
        List<Approval> approvals=new ArrayList<>();
        //时间
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        //查找物业中需要审核的流程
        QueryWrapper<WuyeInfo> wrapper=new QueryWrapper<>();
        wrapper.eq("WY_IS_IDENTIFIED",0);
        List<WuyeInfo> WuyeInfos = wuyeInfoService.list(wrapper);
        //查找商家中需要审核的流程
        QueryWrapper<BusinessInfo> wrapper2=new QueryWrapper<>();
        wrapper2.eq("BUSINESS_STATE",0);
        List<BusinessInfo> businessInfos=businessInfoService.list(wrapper2);
        for (WuyeInfo wuyeInfo:WuyeInfos){
            Approval approval=new Approval();
            //赋值
            approval.setApprovalMessage(wuyeInfo.getWyId().toString());
            approval.setLaunchUserId(wuyeInfo.getWyMan());
            //生成审批单编号
            Integer ran=(int)(Math.random()*90)+10;
            approval.setApprovalId("WY"+df.format(new Date())+ran);
            approval.setApprovalType("物业审批单");
            approvals.add(approval);
            wuyeInfo.setWyIsIdentified(1);
        }

        //查找商家中需要审批的流程


        for (BusinessInfo businessInfo:businessInfos){
            Approval approval=new Approval();
            //赋值
            approval.setApprovalMessage(businessInfo.getBusinessId().toString());
            approval.setLaunchUserId(businessInfo.getUserId());
            //生成审批单编号
            Integer ran=(int)(Math.random()*90)+10;
            approval.setApprovalId("SJ"+df.format(new Date())+ran);
            approval.setApprovalType("商家审批单");
            approvals.add(approval);
            businessInfo.setBusinessState(1);
        }
        //更改状态
        wuyeInfoService.updateBatchById(WuyeInfos);
        businessInfoService.updateBatchById(businessInfos);
        //存入审批单
        approvalService.saveBatch(approvals);




    }
}
